﻿using IntoCommon.Util;
using IntoMes.Global;
using Prism.Commands;
using Prism.Mvvm;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using MessageBox = HandyControl.Controls.MessageBox;


/*===============================================================================
*CreationTime: 2024/9/19 15:08:15
*Creator: HeuberFu
*Description:
*===============================================================================*/
namespace IntoMes.ViewModels
{
    public class AddProductModelWindowVM:BindableBase
    {

        #region Ctor

        public AddProductModelWindowVM()
        {
            List = new List<string>()
            {
                "新增","删除"
            };

            Models = Locator.Instance.SparePartsManage.Recipes;
            CurMode = "新增";
        }


        #endregion Ctor

        #region Property

        #region 当前选择要删除的型号 

        private string _CurDelModel;
        /// <summary>
        /// 当前选择要删除的型号
        /// </summary>
        public string CurDelModel
        {
            get { return _CurDelModel; }
            set
            {
                _CurDelModel = value;
                RaisePropertyChanged("CurDelModel");
            }
        }
        #endregion 当前选择要删除的型号

        #region 型号列表 

        private ObservableCollection<string> _Models;
        /// <summary>
        /// 型号列表
        /// </summary>
        public ObservableCollection<string> Models
        {
            get { return _Models; }
            set
            {
                _Models = value;
                RaisePropertyChanged("Models");
            }
        }
        #endregion 型号列表

        #region 当前选择 

        private string _CurMode;
        /// <summary>
        /// 当前选择
        /// </summary>
        public string CurMode
        {
            get { return _CurMode; }
            set
            {
                _CurMode = value;
                RaisePropertyChanged("CurMode");

                if (value.Equals("新增"))
                {
                    IsAdd = Visibility.Visible;
                    IsDel = Visibility.Hidden;
                }
                else
                {
                    IsAdd = Visibility.Hidden;
                    IsDel = Visibility.Visible;
                }
            }
        }
        #endregion 当前选择

        #region 是否为删除 

        private Visibility _IsDel;
        /// <summary>
        /// 是否为删除
        /// </summary>
        public Visibility IsDel
        {
            get { return _IsDel; }
            set
            {
                _IsDel = value;
                RaisePropertyChanged("IsDel");
            }
        }
        #endregion 是否为删除


        #region 是否为新增 

        private Visibility _IsAdd;
        /// <summary>
        /// 是否为新增
        /// </summary>
        public Visibility IsAdd
        {
            get { return _IsAdd; }
            set
            {
                _IsAdd = value;
                RaisePropertyChanged("IsAdd");
            }
        }
        #endregion 是否为新增

        #region 列表 

        private List<string> _List;
        /// <summary>
        /// 列表
        /// </summary>
        public List<string> List
        {
            get { return _List; }
            set
            {
                _List = value;
                RaisePropertyChanged("List");
            }
        }
        #endregion 列表


        #region 型号名称 

        private string _ProductModelName;
        /// <summary>
        /// 型号名称
        /// </summary>
        public string ProductModelName
        {
            get { return _ProductModelName; }
            set
            {
                _ProductModelName = value;
                RaisePropertyChanged("ProductModelName");
            }
        }
        #endregion 型号名称


        #endregion

        #region Command

        #region 删除 命令

        /// <summary>
        /// 删除命令
        /// </summary>
        public ICommand DeleteCommand => new DelegateCommand(() =>
        {
            if (string.IsNullOrEmpty(CurDelModel))
            {
                MessageBox.Show("未选择型号", "提示", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            var nowModel = Models[Locator.Instance.SparePartsManage.ProductMode];
            if (nowModel.Equals(CurDelModel))
            {
                MessageBox.Show("当前型号正在生产，不能删除", "提示", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            Models.Remove(CurDelModel);
            var res = GlobalFunc.WriteXmlFile(Paths.recipeCfgFile, Models);
            if (res)
            {
                Locator.Instance.SparePartsManage.Recipes.Remove(CurDelModel);
            }
        });

        #endregion 删除



        #region 添加 命令

        /// <summary>
        /// 添加命令
        /// </summary>
        public ICommand AddModelCommand => new DelegateCommand(() =>
        {
            if (string.IsNullOrEmpty(ProductModelName.Trim()))
            {
                MessageBox.Show("未输入型号名称","提示",MessageBoxButton.OK,MessageBoxImage.Error);
                return;
            }

            try
            {
                var models = Locator.Instance.SparePartsManage.Recipes.ToList();
                if(models.Contains(ProductModelName))
                {
                    MessageBox.Show("当前型号已经存在，不能重复添加", "提示", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                models.Add(ProductModelName);
                ObservableCollection<string> arr = new ObservableCollection<string>(models);
                var res = GlobalFunc.WriteXmlFile(Paths.recipeCfgFile, arr);
                if (res)//更新成功，刷新型号列表
                {
                    Locator.Instance.SparePartsManage.Recipes.Add(ProductModelName);
                }
                else
                    MessageBox.Show("添加失败", "提示", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            catch
            {
                //DbHelper.LogTip("添加型号");
            }
        });

        #endregion 添加

        #region 退出 命令

        /// <summary>
        /// 退出命令
        /// </summary>
        public ICommand QuitCommand => new DelegateCommand<HandyControl.Controls.Window>((win) =>
        {
            win.Close();
        });

        #endregion 退出



        #endregion

        #region PublicMethod



        #endregion PublicMethod

        #region PrivateMethod



        #endregion PrivateMethod

    }
}
